package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.FAQ;
import com.drcoffee.drcoffeemall.mapper.FAQMapper;
import com.drcoffee.drcoffeemall.service.FAQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@DS("mysql") // yml中配置的数据源名
public class FAQServiceImpl implements FAQService {
    @Autowired
    FAQMapper faqMapper;

    @Override
    public Map<String, Object> getFAQ(String language, Integer machine_id, String search) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<FAQ> faqList;
            if (machine_id == null) {
                faqList = faqMapper.getFAQBySearch(language,'%' + search + '%');

            } else {
                faqList = faqMapper.getFAQ(language,machine_id, '%' + search + '%');
            }
            resultMap.put("faqList", faqList);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getFaqAll() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<FAQ> faqList = faqMapper.getFaqAll();
            resultMap.put("faqList", faqList);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> updateFaq(FAQ faq) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean result = faqMapper.updateFaq(faq);
            resultMap.put("result", result);
            if (result) {
                resultMap.put("message", "修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> addFAQ(FAQ faq) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = faqMapper.addFAQ(faq);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "添加成功");
        } else {
            resultMap.put("message", "添加失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> deleteFAQ(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = faqMapper.deleteFAQ(id);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "删除成功");
        } else {
            resultMap.put("message", "删除失败");
        }
        return resultMap;
    }
}
